package com.shinhye.demoproject;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.shinhye.autorollviewpager.AutoRollImagePager;
import com.shinhye.autorollviewpager.AutoRollViewPager;

import java.util.ArrayList;

public class MaterialActivity extends AppCompatActivity {

    private SwipeRefreshLayout swipeview;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_material);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        init();
    }

    private void init() {
        TabLayout tablayout = (TabLayout) findViewById(R.id.tablayout);
        ViewPager viewpager = (ViewPager) findViewById(R.id.viewpager);
        adapter = new MyAdapter();
        viewpager.setAdapter(adapter);
        tablayout.setupWithViewPager(viewpager);

        FloatingActionButton f_button = (FloatingActionButton) findViewById(R.id.f_button);
        f_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NestedScrollView sv = (NestedScrollView) findViewById(R.id.sv);
                sv.fullScroll(View.FOCUS_UP);
            }
        });

    }

    private void initSwipe(View layoutview) {
        swipeview = (SwipeRefreshLayout) layoutview.findViewById(R.id.swipe);
        swipeview.setColorSchemeColors(0xFFFF6600, 0xFFDD34DD);
        swipeview.setProgressViewOffset(false, 40, 100);
        swipeview.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeview.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeview.setRefreshing(false);
                    }
                }, 2000);
            }
        });
    }

    private void initAutoRoll(View layoutview) {
        AutoRollImagePager rp_headpic = (AutoRollImagePager) layoutview.findViewById(R.id.rp_headpic);
        rp_headpic.setAutoRoll(true);
        rp_headpic.setLoopMode(true);
        rp_headpic.setRollInterval(2000);
        rp_headpic.setDotInterval(6);
        rp_headpic.setDirection(AutoRollViewPager.DIRECTION_LEFT);

        ArrayList<String> picUrls = new ArrayList<>();
        picUrls.add("https://creditmanager.b0.upaiyun.com/529c2081e3c58002ffed41f5db5873dc");
        picUrls.add("http://creditmanager.b0.upaiyun.com/d7f198f9e98ce4b403c1aa27f10c2712");
        picUrls.add("http://creditmanager.b0.upaiyun.com/d19292c4a842baa4d7288f90700fb56b");

        ArrayList<String> titles = new ArrayList<>();
        titles.add("汪磊好");
        titles.add("汪磊帅");
        titles.add("汪磊棒");

        // 与网络图片关联
        rp_headpic.setImageUrl(picUrls);
        rp_headpic.setTitles(titles);
        rp_headpic.setOnPageClickListener(new AutoRollViewPager.OnPagerClick() {
            @Override
            public void onClick(int position) {
                Toast.makeText(MaterialActivity.this, "position " + position + " clicked", Toast.LENGTH_SHORT).show();
            }
        });
        rp_headpic.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                Log.i("OnPageChange", "onPageSelected::position==" + position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                Log.i("OnPageChange", "onPageScrollStateChanged::state==" + state);
            }
        });
        rp_headpic.showUp();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.action_settings:
                startActivity(new Intent(this, SettingsActivity.class));
                return true;
            case android.R.id.home:
                finish();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if(position == 0) {
                return "SwipeNested";
            } else {
                return "Testin";
            }
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            if(position == 0) {
                View layoutview = View.inflate(MaterialActivity.this, R.layout.content_material, null);
                initSwipe(layoutview);
                initAutoRoll(layoutview);
                container.addView(layoutview);
                return layoutview;
            } else {
                NestedScrollView sv = new NestedScrollView(MaterialActivity.this);
                sv.setId(R.id.sv);
                sv.setVerticalScrollBarEnabled(true);

                TextView textView = new TextView(MaterialActivity.this);
                textView.setText(R.string.large_text);
                textView.setGravity(Gravity.CENTER);

                sv.addView(textView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                container.addView(sv, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                return sv;
            }
        }
    }
}
